﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.google.zxing.qrcode;
using com.google.zxing;
using com.google.zxing.common;
using ByteMatrix = com.google.zxing.common.ByteMatrix;
using EAN13Writer = com.google.zxing.oned.EAN13Writer;
using EAN8Writer = com.google.zxing.oned.EAN8Writer;
using System.Reflection;

namespace ZXingApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ByteMatrix byteMatrix;
        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 450; i++)
            {
                comboBox1.Items.Add(i);
                comboBox2.Items.Add(i);
            }
            comboBox1.SelectedIndex = 449;
            comboBox2.SelectedIndex = 449;

            Type type = typeof(Color);
            PropertyInfo[] myProperties = type.GetProperties(BindingFlags.Static | BindingFlags.Public);
            foreach (PropertyInfo item in myProperties)
            {
                comboBox3.Items.Add(item.Name);
                comboBox4.Items.Add(item.Name);
            }
            comboBox3.SelectedIndex = 8;
            comboBox4.SelectedIndex = 137;
        }

        public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
        {
            Bitmap bmap = toBitmap(matrix, ColorTranslator.FromHtml("0xFF000000"), ColorTranslator.FromHtml("0xFFFFFFFF"));
            bmap.Save(file, format);
        }
        public static Bitmap toBitmap(ByteMatrix matrix, Color color1, Color color2)
        {
            int width = matrix.Width;
            int height = matrix.Height;
            Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? color1 : color2);
                }
            }
            return bmap;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string content = textBox1.Text;
            if (string.IsNullOrEmpty(content))
            {
                MessageBox.Show("请输入内容");
                textBox1.Focus();
                return;
            }
            int height = Convert.ToInt32(comboBox2.SelectedItem);
            int width = Convert.ToInt32(comboBox1.SelectedItem);
            byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
            Color color1 = Color.FromName(comboBox3.SelectedItem.ToString());
            Color color2 = Color.FromName(comboBox4.SelectedItem.ToString());
            Bitmap bitmap = toBitmap(byteMatrix, color1, color2);
            pictureBox1.Image = bitmap;
            button2.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sFD = new SaveFileDialog();
            sFD.Filter = "PNG|*.png";
            sFD.InitialDirectory = "c:\\";
            try
            {
                if (sFD.ShowDialog() == DialogResult.OK)
                {
                    writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
            openFileDialog.Filter = "PNG|*.png";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog.FileName;
                Image img = Image.FromFile(openFileDialog.FileName);
                Bitmap bmap;
                try
                {
                    bmap = new Bitmap(img);
                }
                catch (System.IO.IOException ioe)
                {
                    MessageBox.Show(ioe.ToString());
                    return;
                }
                if (bmap == null)
                {
                    MessageBox.Show("不能解析该二维码。");
                    return;
                }
                LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
                com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
                Result result;
                try
                {
                    result = new MultiFormatReader().decode(bitmap);
                }
                catch (ReaderException re)
                {
                    MessageBox.Show("图片太大，请生成450*450以下的二维码。");
                    return;
                }
                label5.Text = result.Text;
            }
        }
    }
}
